package icselab.dbaccess;

import icselab.util.ConfigParser;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;

public class MonitorDAO {
	
	private String connStr = null;
	private String username = null;
	private String password = null;
	private Connection conn = null;
	
	public MonitorDAO(){
		ConfigParser parser = new ConfigParser();
		connStr = parser.getElement("connectionstring");
		username = parser.getElement("username");
		password = parser.getElement("password");

		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(connStr, username, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean updateMonitorStat(String InstanceId,int ServletId,int min,int max,float avg, int totalReq)
	{
		CallableStatement statement;
		try {
			statement = conn.prepareCall("{call sp_updateMonitorStat(?,?,?,?,?,?,?)}");
			statement.setString(1, Long.toString(System.currentTimeMillis()));
			statement.setString(2, InstanceId);
			statement.setInt(3, ServletId);
			statement.setInt(4, min);
			statement.setInt(5, max);
			statement.setFloat(6, avg);
			statement.setInt(7, totalReq);
			statement.execute();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteMonitorStat(String InstanceId)
	{
		CallableStatement statement;
		try {
			statement = conn.prepareCall("{call sp_deleteMonitorByInstId(?)}");
			statement.setString(1, InstanceId);
			statement.execute();
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
